home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / vs / vsintern.c < prev    next >
Text File  |  1993-12-20  |  37KB  |  1,431 lines

  1. /*
  2.  *
  3.  *      Virtual Screen Kernel Internal Routines
  4.  *                      (vsintern.c)
  5.  *  National Center for Supercomputing Applications
  6.  *
  7.  *      by Gaige B. Paulsen
  8.  *
  9.  *    This file contains the private internal calls for the NCSA
  10.  *  Virtual Screen Kernel.
  11.  *
  12.  *        Version Date    Notes
  13.  *        ------- ------  ---------------------------------------------------
  14.  *        0.01    861102  Initial coding -GBP
  15.  *        0.50    861113  First compiled edition -GBP
  16.  *        0.70    861114  Internal operation confirmed -GBP
  17.  *        2.1        871130    NCSA Telnet 2.1 -GBP
  18.  *        2.2     880715    NCSA Telnet 2.2 -GBP
  19.  */
  20.  
  21. #ifdef MPW
  22. #pragma segment VS
  23. #endif
  24.  
  25. #include "TelnetHeader.h"
  26.  
  27. #include <String.h>
  28. #include <Quickdraw.h>
  29. #include <Controls.h>
  30. #include <Memory.h>
  31. #include <OSUtils.h>                /* LU */
  32. #include <stdio.h>
  33. #include "vsdata.h"
  34. #include "vskeys.h"
  35. #include "vsinterf.proto.h"
  36. #include "rsmac.proto.h"
  37. #include "maclook.proto.h"
  38. #include "wind.h"
  39.  
  40. #define ScrollbackQuantum 100
  41.  
  42. #define VSIclrattrib 0
  43.  
  44. #include "vsintern.proto.h"
  45.  
  46. extern SysEnvRec    theWorld;        /* BYU 2.4.12 - System Environment record */
  47. extern short     TempItemsVRefNum;
  48. extern long        TempItemsDirID;
  49. extern WindRec *screens;
  50.  
  51. short VSIclip
  52.   (
  53.      short *x1, /* starting column */
  54.      short *y1, /* line on which to draw (assumed to lie within visible region) */
  55.      short *x2, /* ending column (inclusive) (output if *n >= 0) */
  56.      short *y2, /* ending line (inclusive) (output if *n >= 0) */
  57.      short *n, /* length of text to draw (input and output) */
  58.      short *offset /* length of initial part of text to skip (output) */
  59.   )
  60.   /* clips a text string to the visible region, given the starting
  61.     line and column in screen coordinates at which it is to be drawn.
  62.     If the length of the string is given, will also compute the ending
  63.     line and column. On return, these coordinates will be normalized
  64.     to the current visible region. Returns a nonzero function result
  65.     iff the string is completely invisible. */
  66.   {
  67.     if (*n >= 0)
  68.       {
  69.       /* compute ending line and column (inclusive) */
  70.         *x2 = *x1 + *n - 1;
  71.         *y2 = *y1;
  72.       }
  73.   /* else take these as given */
  74.  
  75.     if ((*x1 > VSIw->Rright) || (*y2 < VSIw->Rtop))
  76.         return (-1); /* nothing to draw */
  77.  
  78.     if (*x2 > VSIw->Rright)
  79.         *x2 = VSIw->Rright;
  80.     if (*y2 > VSIw->Rbottom)
  81.         *y2 = VSIw->Rbottom;
  82.   /* normalize x1, x2, y1, y2 to be relative to current visible region */
  83.     *x1 -= VSIw->Rleft;
  84.     *x2 -= VSIw->Rleft;
  85.     *y1 -= VSIw->Rtop;
  86.     *y2 -= VSIw->Rtop;
  87.   /* clip part of text string lying outside region, if any */
  88.     *offset = - *x1;
  89.     if (*offset < 0)
  90.         *offset = 0; /* text string starts within region--nothing to clip */
  91.   /* don't draw anything outside region */
  92.     if (*x1 < 0)
  93.         *x1 = 0;
  94.     if (*y1 < 0)
  95.         *y1 = 0;
  96.  
  97.     *n = *x2 - *x1  + 1 ; /* length of string to draw (assuming it's all on one line) */
  98.     if ((*n <= 0) || (*y2 - *y1 < 0))
  99.         return (-1); /* nothing to draw */
  100.     return (0);
  101.   } /* VSIclip */
  102.  
  103. short VSIcdellines(short w, short top, short bottom, short n, short scrolled)
  104.           /*
  105.             -ve => cancel current selection, if any;
  106.             +ve => selection has moved up one line;
  107.             0 => don't touch selection
  108.           */
  109.   /* updates the display to indicate deletion of the specified
  110.     number of lines from the top of the specified region.
  111.     Returns 0 iff any part of the change is visible. */
  112.   {
  113.     short
  114.         x1 = 0,
  115.         x2 = VSIw->maxwidth,
  116.         tn = -1,
  117.         offset;
  118.  
  119.     if (VSIclip(&x1, &top, &x2, &bottom, &tn, &offset))
  120.         return(-1); /* affected region is invisible */
  121.     tn = bottom - top;
  122.     if (tn < n)
  123.         n = tn; /* don't bother scrolling more lines than scrolling region holds */
  124.     RSdellines(w, top, bottom, n, scrolled);
  125.     return(0);                /* I delete the whole thing! */
  126.   } /* VSIcdellines */
  127.  
  128. short VSIcinslines(short w, short top, short bottom, short n, short scrolled) /* -ve <=> cancel current selection, if any */
  129.   /* updates the display to indicate insertion of the specified
  130.     number of blank lines at the top of the specified region.
  131.     Returns 0 iff any part of the change is visible. */
  132.   {
  133.     short
  134.         x1 = 0,
  135.         x2 = VSIw->maxwidth,
  136.         tn = -1,
  137.         offset;
  138.  
  139.     if (VSIclip(&x1, &top, &x2, &bottom, &tn, &offset))
  140.         return -1; /* affected region is invisible */
  141.     tn = bottom - top;
  142.     if (tn < n)
  143.         n = tn; /* don't bother scrolling more lines than scrolling region holds */
  144.     RSinslines(w, top, bottom, n, scrolled);
  145.     return 0;
  146.   } /* VSIcinslines */
  147.  
  148. void VSIcurson
  149.   (
  150.     short w,
  151.     short x,
  152.     short y,
  153.     short ForceMove
  154.   )
  155.   /* displays the text cursor at the specified position. If
  156.     ForceMove is true, I am to do any appropriate scrolling of
  157.     the display to ensure the cursor is within the visible region.
  158.     Assumes cursor isn't currently being shown. */
  159.   {
  160.     short
  161.         x2,
  162.         y2,
  163.         n = 1,
  164.         offset;
  165.  
  166.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))
  167.       /* cursor already lies within visible region */
  168.         RScurson(w, x, y); /* just make it visible */
  169.     else if (ForceMove)
  170.       {
  171.       /* scroll to make cursor visible */
  172.         x2 = VSIw->Rbottom - VSIw->Rtop;
  173.         if (x2 >= VSIw->lines)
  174.           /* visible region is big enough to show entire screen--
  175.             make sure I don't scroll off the bottom of the screen.
  176.             This call will also do any appropriate scrolling and
  177.             redisplaying of the cursor. */
  178.             VSsetrgn(VSIwn, VSIw->Rleft, VSIw->lines - x2,
  179.                 VSIw->Rright, VSIw->lines);
  180.         else
  181.           {
  182.           /* x & y have been normalized relative to left & top
  183.             of current visible region. Just call the appropriate scroll
  184.             routine, which will also redisplay the cursor. */
  185.             if (y > 0)
  186.                 VSscrolforward(VSIwn, y);
  187.             else
  188.                 VSscrolback(VSIwn, -y);
  189.           } /* if */
  190.       } /* if */
  191.   } /* VSIcurson */
  192.  
  193. void VSIcuroff
  194.   (
  195.     short w
  196.   )
  197.   /* hides the cursor for the specified screen. Assumes it
  198.     is currently being shown (or that it's on an invisible
  199.     part of the screen). */
  200.   {
  201.     short
  202.         x = VSIw->x,
  203.         y = VSIw->y,
  204.         x2,
  205.         y2,
  206.         n = 1,
  207.         offset;
  208.  
  209.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))
  210.       /* cursor is on visible part of screen */
  211.         RScursoff(w);
  212.   } /* VSIcuroff */
  213.  
  214. short VSIcursorvisible        /* BYU 2.4.12 */
  215.   (                            /* BYU 2.4.12 */
  216.     void                    /* BYU 2.4.12 */
  217.   )                            /* BYU 2.4.12 */
  218.   {                            /* BYU 2.4.12 */
  219.     short                        /* BYU 2.4.12 */
  220.         x = VSIw->x,        /* BYU 2.4.12 */
  221.         y = VSIw->y,        /* BYU 2.4.12 */
  222.         x2,                    /* BYU 2.4.12 */
  223.         y2,                    /* BYU 2.4.12 */
  224.         n = 1,                /* BYU 2.4.12 */
  225.         offset;                /* BYU 2.4.12 */
  226.                                                             /* BYU 2.4.12 */
  227.     if (!VSIclip(&x, &y, &x2, &y2, &n, &offset))            /* BYU 2.4.12 */
  228.         return 1;                                            /* BYU 2.4.12 */
  229.     else                    /* BYU 2.4.12 */
  230.         return 0;            /* BYU 2.4.12 */
  231. }                            /* BYU 2.4.12 */
  232.  
  233. VSlineArray VSInewlinearray
  234.   (
  235.     short nrlines
  236.   )
  237.   /* allocates an array to hold the specified number of pointers
  238.     to line elements. */
  239.   {
  240.     return
  241.         (VSlineArray) NewPtrClear(sizeof(VSlinePtr) * nrlines);
  242.   } /* VSInewlinearray */
  243.  
  244. VSlinePtr VSInewlines
  245.   (
  246.     short nlines
  247.   )
  248.   /* allocates a doubly-linked list of the specified number of
  249.     line elements, and returns a pointer to the head of the list,
  250.     or nil if ran out of memory. The maximum number of characters
  251.     each line can hold is controlled by VSIw->allwidth. */
  252.   {
  253.     VSlinePtr t2;
  254.     char *t;
  255.     register short i;
  256.     
  257.     if (!RSokmem(30000))                /* don't use up end of mem for scrollback */
  258.         return((VSlinePtr) 0L);
  259.   /* allocate one block for the line list elements, and another
  260.     block for the line contents. These blocks will be divided up
  261.     and appropriate flags set so I will be able to call DisposPtr
  262.     the right number of times, with the right arguments. */
  263.     if ((t = NewPtr(nlines * (VSIw->allwidth + 1))) != 0L)
  264.       {
  265.         if ((t2 = (VSlinePtr) NewPtr(nlines * sizeof(VSline))) != 0L)
  266.             t2->text = t;
  267.         else
  268.           {
  269.           /* clean up gracefully before giving up */
  270.             DisposPtr(t);
  271.             return(0L);
  272.           } /* if */
  273.       }
  274.     else
  275.       /* sorree no memoree */
  276.         return((VSlinePtr) 0L);
  277.  
  278. /*
  279. *  indicate to the free routine that the first record is the one to free.
  280. */
  281.     t2->mem = 1;                        /* call DisposPtr on this one */
  282.     t2->next = t2 + 1;                    /* point to next one */
  283. /*
  284. *  Take our allocation for multiple lines and fill in the structures to 
  285. *  point to the right text fields and connect the doubly-linked chain.
  286. *
  287. */
  288.     for (i = 1; i < nlines; i++)
  289.       {
  290.         t += (VSIw->allwidth + 1);        /* inc to next text space for a line */
  291.         t2[i].mem = 0;                    /* don't DisposPtr any of these */
  292.         t2[i].text = t;
  293.         t2[i].prev = t2 + i - 1;        /* point back one */
  294.         t2[i].next = t2 + i + 1;        /* point forward one */
  295.       } /* for */
  296.     
  297.     t2[0].prev = 0L;                    /* first one has no prev yet */
  298.     t2[nlines - 1].next = 0L;            /* last one has no next yet */
  299.  
  300.     return(t2);
  301.   } /* VSInewlines */
  302.  
  303. void VSIlistndx
  304.   (
  305.     register VSlinePtr ts,
  306.     register VSlinePtr as
  307.   )
  308.   /* sets up the screen arrays for the current screen to point
  309.     at the given lists of attribute and text lines. */
  310.   {
  311.     register short i;
  312.     for (i = 0; i <= VSIw->lines; i++)
  313.       {
  314.         VSIw->attrst[i] = as;
  315.         VSIw->linest[i] = ts;
  316.         ts = ts->next;
  317.         as = as->next;
  318.       } /* for */
  319.   } /* VSIlistndx */
  320.  
  321. void VSIscroff
  322.   (
  323.     void
  324.   )
  325.   /* called to save current screen contents in scrollback buffer,
  326.     if it is ordained that I should do so. This is called by VSIes
  327.     (below) just before the entire screen is cleared. */
  328.   {
  329.     VSlinePtr tmp;
  330.     register short i, j;
  331.  
  332.     if
  333.       (
  334.             (!VSIw->savelines) /* not saving lines */
  335.         ||
  336.             (VSIw->top != 0) || (VSIw->bottom != VSIw->lines)
  337.               /* scrolling region isn't exactly the entire screen */
  338.       )
  339.         return; /* do nothing */
  340.  
  341.     tmp = VSIw->linest[VSIw->lines]; /* need VSIw->lines + 1 more lines */
  342.     for (i = 0; i <= VSIw->lines; i++)
  343.       {
  344.       /* count off the lines in the part of the scrollback buffer
  345.         below the screen (if any), to see if there's enough to hold
  346.         a screenful. If the scrollback list isn't circular, then
  347.         this part contains lines that have been allocated, but not
  348.         yet used. If the list is circular (meaning it has reached
  349.         its full size), then this is the part that is next in line
  350.         for reuse. */
  351.         if (!tmp->next) 
  352.           { /* not enough */
  353.             j = VSIw->maxlines - VSIw->numlines - i; /* potential unallocated scrollback */
  354.             if (j > ScrollbackQuantum)
  355.                 j = ScrollbackQuantum; /* but don't bother allocating more than this */
  356.             if (j <= 0)
  357.               {
  358.               /* already reached user-specified scrollback limit-- */
  359.               /* make the list circular to indicate no more extension. */
  360.                 tmp->next = VSIw->buftop;
  361.                 VSIw->buftop->prev = tmp;        /* connect it up */
  362.               }
  363.             else
  364.               {
  365.               /* extend the scrollback buffer to make room for
  366.                 another screenful */
  367.                 if (j < VSIw->lines - i + 1)
  368.                     j = VSIw->lines - i + 1; /* need at least this many */
  369.                 if ((tmp->next = VSInewlines(j)) != 0L) 
  370.                     tmp->next->prev = tmp;        /* got some space--link it up */
  371.                 else
  372.                   {
  373.                   /* out of memory--no more extensions */
  374.                     tmp->next = VSIw->buftop;
  375.                     VSIw->buftop->prev = tmp;    
  376.                   } /* if */                
  377.               } /* if */
  378.             break;                                /* only allocate once is enough */
  379.           } /* if */
  380.         tmp = tmp->next; /* keep counting */
  381.       } /* for */
  382.         
  383. /*
  384. *  at this point, we know we have enough memory for the whole scroll.
  385. *  It might be wraparound (reuse of some line elements), might not.
  386. */
  387.         
  388.     for (i = 0; i <= VSIw->lines; i++)
  389.       {
  390.       /* push another screen line into the scrollback area */
  391.         if (VSIw->linest[VSIw->lines]->next == VSIw->buftop)
  392.             VSIw->buftop = VSIw->buftop->next;    /* reusing old space */
  393.         else
  394.             VSIw->numlines++;                /* using some new space */
  395.         VSIw->scrntop = VSIw->scrntop->next; /* move another line into the scrollback buffer */
  396.         VSIlistndx(VSIw->scrntop, VSIw->attrst[1]); /* and update screen arrays */
  397.       /* note that it's up to the caller to clear out the new screen text
  398.         and attribute lines */
  399.       } /* for */
  400.  
  401.     VSIw->vistop = VSIw->scrntop;
  402.     RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar */
  403.   } /* VSIscroff */
  404.  
  405. void VSIelo
  406.   (
  407.     short s /* line to erase, -ve => line containing cursor */
  408.   )
  409.   /* blanks out the specified line in the screen buffer.
  410.     Doesn't do anything to the display. */
  411.   {
  412.     char *tt, *ta;
  413.     short i;
  414.  
  415.     if (s < 0)
  416.         s = VSIw->y;
  417.  
  418.     ta = &VSIw->attrst[s]->text[0];
  419.     tt = &VSIw->linest[s]->text[0];
  420.     for (i = 0; i <= VSIw->allwidth; i++)
  421.       {
  422.         *ta++ = VSIclrattrib;
  423.         *tt++ = ' ';
  424.       } /* for */
  425.   } /* VSIelo */
  426.  
  427. void VSIes
  428.   (
  429.     void
  430.   )
  431.   /* clears the screen, first saving its contents in the
  432.     scrollback buffer if appropriate. Also updates the display. */
  433.   {
  434.     short
  435.         i;
  436.     short
  437.         x1 = 0,
  438.         y1 = 0,
  439.         x2 = VSIw->maxwidth,
  440.         y2 = VSIw->lines,
  441.         n = -1,
  442.         offset;
  443.  
  444.   /* save screen contents in scrollback buffer, if appropriate */
  445.     if (VSIw->ESscroll)
  446.         VSIscroff();
  447.   /* clear out screen buffer */
  448.     for (i = 0; i <= VSIw->lines; i++)
  449.         VSIelo(i);
  450.   /* update display to show what I've done */
  451.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  452.         RSerase(VSIwn, x1, y1, x2, y2);
  453.     VSIw->vistop = VSIw->scrntop;
  454.   } /* VSIes */
  455.  
  456. void VSItabclear
  457.   (
  458.     void
  459.   )
  460.   /* clears all current tab settings. */
  461.   {
  462.     short
  463.         x = 0;
  464.  
  465.     while (x <= VSIw->allwidth)
  466.       {
  467.         VSIw->tabs[x] = ' ';
  468.         x++;
  469.       } /* while */
  470.   } /* VSItabclear */
  471.  
  472. void VSItabinit
  473.   (
  474.     void
  475.   )
  476.   /* sets tab settings to default (every 8 columns). */
  477.   {
  478.     short
  479.         x = 0;
  480.  
  481.     VSItabclear();
  482.     while (x <= VSIw->allwidth)
  483.       {
  484.         VSIw->tabs[x] = 'x';
  485.         x += 8;
  486.       }
  487.     VSIw->tabs[VSIw->allwidth] = 'x';
  488.   } /* VSItabinit */
  489.  
  490. void VSIreset
  491.   (
  492.     void
  493.   )
  494.   /* restores terminal mode settings to defaults and clears screen. */
  495.   {
  496.     VSIw->top = 0;
  497.     VSIw->bottom = VSIw->lines;
  498.     VSIw->parmptr = 0;
  499.     VSIw->escflg = 0;
  500.     VSIw->DECAWM = 0;
  501.     VSIw->DECCKM = 0;
  502.     VSIw->DECPAM = 0;
  503.     VSIw->DECORG = 0;        /* NCSA: SB -- is this needed? */
  504.     VSIw->Pattrib = -1;        /* NCSA: SB -- is this needed? */
  505.     VSIw->IRM = 0;
  506.     VSIw->attrib = 0;
  507.     VSIw->x = 0;
  508.     VSIw->y = 0;
  509.     VSIw->charset = 0;
  510.     VSIw->prbuf=0;                                        /* LU */
  511.     if (VSIw->prredirect) {                                /* LU - kill redirection */
  512.         VSIw->prredirect=0;                                /* LU */
  513.         FSClose (VSIw->refNum);                            /* LU */
  514.         VSIw->refNum=-1;                                /* LU */
  515.         HDelete(TempItemsVRefNum, TempItemsDirID, (StringPtr)VSIw->fname);        /* LU */
  516.     }                                                    /* LU */
  517.     VSIes();
  518.     VSItabinit();
  519.     } /* VSIreset */
  520.  
  521. void VSIlistmove(VSlinePtr TD, VSlinePtr BD, VSlinePtr TI, VSlinePtr BI)
  522.   /* moves the lines from TD to BD inclusive from their
  523.     present position to between TI and BI. Either of the
  524.     latter may be nil. */
  525.   {
  526.   /* unlink the lines from TD to BD */
  527.     if (TD->prev != 0L)
  528.         TD->prev->next = BD->next;    /* Maintain circularity */
  529.     if (BD->next != 0L)
  530.         BD->next->prev = TD->prev;
  531.   /* relink them between TI and BI */
  532.     TD->prev = TI;                                /* Place the node in its new home */
  533.     BD->next = BI;
  534.     if (TI != 0L)
  535.         TI->next = TD;                    /* Ditto prev->prev */
  536.     if (BI != 0L)
  537.         BI->prev = BD;
  538.   } /* VSIlistmove */
  539.  
  540. void VSIdellines
  541.   (
  542.     short n, /* nr lines to delete */
  543.     short s /* starting line to delete, -ve => line containing cursor */
  544.   )
  545.   /* deletes lines from the screen, scrolling up the remainder and
  546.     inserting new blank lines at the bottom of the scrolling region. */
  547.   {
  548.     short i, j;
  549.     char *ta, *tt;
  550.     VSlinePtr as, ts, TD, BD, TI, BI, itt, ita;
  551.  
  552.     if (s < 0)
  553.         s = VSIw->y;
  554.     if (s + n - 1 > VSIw->bottom)
  555.         n = VSIw->bottom - s + 1; /* don't bother deleting more than scrolling region will hold */
  556.  
  557.   /* find new tops of screen arrays */
  558.     if (s == 0 && n <= VSIw->lines)
  559.       {
  560.       /* element for line after last one being deleted */
  561.         ts = VSIw->linest[n];
  562.         as = VSIw->attrst[n];
  563.       }
  564.     else
  565.       {
  566.       /* top line unaffected, or entire screen is being wiped */
  567.         ts = VSIw->linest[0];
  568.         as = VSIw->attrst[0];
  569.       } /* if */
  570.  
  571.     TD = VSIw->linest[s]; /* topmost line to delete */
  572.     BD = VSIw->linest[s + n - 1]; /* bottommost line to delete */
  573.     TI = VSIw->linest[VSIw->bottom]; /* insert replacement blank lines after this line */
  574.     BI = TI->next; /* insert them before this line (might be nil) */
  575.     itt = TD; /* start of text lines to be blanked out */
  576.   /* the space taken by the deleted lines will be reused for
  577.     the inserted blank lines */
  578.     if (TD != BI && TI != BD)
  579.       /* insertion and deletion areas not adjacent -- move the lines to
  580.         their new position */
  581.         VSIlistmove(TD, BD, TI, BI);
  582.  
  583.     TD = VSIw->attrst[s]; /* topmost line to delete */
  584.     BD = VSIw->attrst[s + n - 1]; /* bottommost line to delete */
  585.     TI = VSIw->attrst[VSIw->bottom]; /* insert new lines after this one */
  586.     BI = TI->next; /* insert them before this line */
  587.   /* perform same rearrangement on attribute lines as on text lines */
  588.     if (TD != BI && TI != BD)
  589.       /* insertion and deletion areas not adjacent -- move the lines to
  590.         their new position */
  591.         VSIlistmove(TD, BD, TI, BI);
  592.  
  593.   /* blank out the newly-created replacement lines */
  594.     ita = TD; /* start of attribute lines to be blanked out */
  595.     for (i = 0; i < n; i++)
  596.       {
  597.         ta = ita->text;
  598.         tt = itt->text;
  599.         for (j = 0; j <= VSIw->allwidth; j++)
  600.           {
  601.             *tt++ = ' ';
  602.             *ta++ = VSIclrattrib;
  603.           } /* for */
  604.         ita = ita->next;
  605.         itt = itt->next;
  606.       } /* for */
  607.  
  608.     VSIw->scrntop = ts; /* new topmost line (if it's changed) */
  609.   /* re-sync screen arrays */
  610.     VSIlistndx(ts, as);
  611.     if (VSIw->Rtop >= 0)
  612.       /* make sure vistop still points to same line position
  613.         on screen that it did before */
  614.         VSIw->vistop = VSIw->linest[VSIw->Rtop];
  615.   /* and actually display the change on-screen */
  616.     VSIcdellines(VSIwn, s, VSIw->bottom, n, -1); /* Cancel current selection */
  617.   } /* VSIdellines */
  618.  
  619. void VSIinslines
  620.   (
  621.     short n, /* how many to insert */
  622.     short s /* where to insert them, -ve => line containing cursor */
  623.   )
  624.   /* inserts the specified number of blank lines, scrolling the
  625.     remaining ones down, and dropping off any that fall off the
  626.     end of the scrolling region. */
  627.   {
  628.     short i, j;
  629.     char *ta, *tt;
  630.     VSlinePtr as, ts, TD, BD, TI, BI, itt, ita;
  631.  
  632.     if (s < 0)
  633.         s = VSIw->y;
  634.     if (s + n - 1 > VSIw->bottom)
  635.       /* don't bother inserting more than scrolling region can hold */
  636.         n = VSIw->bottom - s + 1;
  637.  
  638.   /* find new tops of screen arrays */
  639.     if (s == 0 && n <= VSIw->lines)
  640.       {
  641.       /* element for first blank line being inserted */
  642.         ts = VSIw->linest[VSIw->bottom - n + 1];
  643.         as = VSIw->attrst[VSIw->bottom - n + 1];
  644.       }
  645.     else
  646.       {
  647.       /* top line unaffected, or entire screen is being wiped */
  648.         ts = VSIw->linest[0];
  649.         as = VSIw->attrst[0];
  650.       } /* if */
  651.  
  652.     BI = VSIw->linest[s]; /* insert blank lines before this one */
  653.     TI = BI->prev; /* insert them after this one */
  654.     TD = VSIw->linest[VSIw->bottom - n + 1]; /* topmost line to delete */
  655.     BD = VSIw->linest[VSIw->bottom]; /* bottommost line to delete */
  656.     itt = TD; /* start of text lines to be blanked out */
  657.   /* the space occupied by the deleted lines will be reused for
  658.     the new blank lines */
  659.     if (TD != BI && TI != BD)
  660.       /* new and deleted lines not contiguous -- move the space
  661.         to its new position */
  662.         VSIlistmove(TD, BD, TI, BI);
  663.  
  664.     BI = VSIw->attrst[s]; /* insert new lines before this one */
  665.     TI = BI->prev; /* insert them after this one */
  666.     TD = VSIw->attrst[VSIw->bottom - n + 1]; /* topmost line to delete */
  667.     BD = VSIw->attrst[VSIw->bottom]; /* bottommost line to delete */
  668.   /* do the same rearrangement on the attribute lines */
  669.     if (TD != BI && TI != BD)
  670.       /* new and deleted lines not contiguous -- move the space
  671.         to its new position */
  672.         VSIlistmove(TD, BD, TI, BI);
  673.  
  674.   /* blank out the newly-inserted lines */
  675.     ita = TD; /* start of attribute lines to be blanked out */
  676.     for (i = 0; i < n; i++)
  677.       {
  678.         tt = itt->text;
  679.         ta = ita->text;
  680.         for (j = 0; j <= VSIw->allwidth; j++)
  681.           {
  682.             *tt++ = ' ';
  683.             *ta++ = VSIclrattrib;
  684.           }
  685.         itt = itt->next;
  686.         ita = ita->next;
  687.       } /* for */
  688.  
  689.     VSIw->scrntop = ts;
  690.     VSIlistndx(ts, as); /* re-sync screen arrays */
  691.     if (VSIw->Rtop >= 0)
  692.       /* make sure vistop still points to same line position
  693.         on screen that it did before */
  694.         VSIw->vistop = VSIw->linest[VSIw->Rtop];
  695.   /* update display to match reality */
  696.     VSIcinslines(VSIwn, s, VSIw->bottom, n, -1);  /* Destroy selection area if this is called tooo */
  697.   } /* VSIinslines */
  698.  
  699. void VSIscroll
  700.   (
  701.     void
  702.   )
  703.   /* scrolls scrolling region up one line. */
  704.   {
  705.     register char *temp, *tempa;
  706.     VSlinePtr tmp;
  707.     register short i;
  708. //    short tx1, tx2, ty1, ty2, tn, offset;
  709.     short theBottom;                /* NCSA 2.5: the correct screen bottom */
  710.  
  711.     if (VSIw->y > VSIw->lines)        /* BYU - replaces BYU modification below */
  712.         return;                        /* BYU */
  713.  
  714. //    tx1 = ty1 = 0;
  715. //    tn = 132;
  716. //    if (!VSIclip(&tx1, &ty1, &tx2, &ty2, &tn, &offset))
  717.       /* top line of screen is visible */
  718. //        RSdrawsep(VSIwn, ty1, 1);                    /* Draw Separator */
  719.  
  720. #if 0                                                        /* BYU */
  721. /* BYU mod - Don't scroll if the cursor is outside of the scrolling region */
  722.     if ((VSIw->y < VSIw->top) || (VSIw->y > VSIw->bottom))    /* BYU */
  723.         return;                                                /* BYU */
  724. #endif                                                        /* BYU */
  725.  
  726.     if ((!VSIw->savelines) /* no scrollback */    ||    (VSIw->top != 0)     /* NCSA 2.5 */
  727.             || ((VSIw->bottom != VSIw->lines) && !VSIw->forcesave))    /* NCSA 2.5 */
  728.               /* region being scrolled is not entire screen */        
  729.  
  730.       /* no saving of lines */
  731.         VSIdellines(1, VSIw->top);
  732.     else
  733.       {
  734.       /* scrolling region is entire screen, and lines are being saved off top */
  735.         if (VSIw->linest[VSIw->lines]->next == 0L)
  736.           {
  737.           /* all currently-allocated scrollback lines have been used, but
  738.             scrollback buffer isn't at its full size -- allocate some more
  739.             space */
  740.             i = VSIw->maxlines - VSIw->numlines; /* number of lines that can be allocated */
  741.             if (i > ScrollbackQuantum)
  742.                 i = ScrollbackQuantum; /* don't bother allocating more than this at once */
  743.             if ((i > 0) && (tmp = VSInewlines(i)) != 0L)
  744.               {
  745.               /* link newly-allocated lines into the list */
  746.                 VSIw->linest[VSIw->lines]->next = tmp;
  747.                 tmp->prev = VSIw->linest[VSIw->lines];
  748.                 VSIw->numlines++; /* use one of the newly-allocated scrollback lines */
  749.                 RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar accordingly */
  750.               }
  751.             else
  752.               {
  753.               /* not enough memory to extend scrollback buffer--reuse
  754.                 oldest line and give up on future extensions */
  755.                 VSIw->linest[VSIw->lines]->next = VSIw->buftop;        /* Make it circular */
  756.                 VSIw->buftop->prev = VSIw->linest[VSIw->lines];
  757.                 VSIw->buftop = VSIw->buftop->next;    /* step one forward */
  758.               } /* if */
  759.           }    
  760.         else
  761.           {
  762.           /* either there's allocated, but not yet used, space at
  763.             VSIw->linest[VSIw->lines]->next, or the text line list
  764.             is circular. Either way, don't do any new scrollback
  765.             allocation. */
  766.             if (VSIw->linest[VSIw->lines]->next == VSIw->buftop)
  767.               /* scrollback buffer is at full size--reuse oldest line */
  768.                 VSIw->buftop = VSIw->buftop->next;
  769.             else
  770.               {
  771.               /* haven't used up all the space I allocated last time */
  772.                 VSIw->numlines++;                    /* count another line */
  773.                 RSbufinfo(VSIwn, VSIw->numlines, VSIw->Rtop, VSIw->Rbottom); /* update vertical scroll bar accordingly */
  774.               } /* if */
  775.           } /* if */
  776.  
  777.         VSIw->scrntop = VSIw->scrntop->next; /* scroll the screen buffer */
  778.         VSIlistndx(VSIw->scrntop, VSIw->attrst[1]); /* update screen arrays */
  779.       /* reflect the change in the display by scrolling up the visible
  780.         part of the on-screen area, if any */
  781.  
  782.  
  783.         if (VSIw->forcesave) theBottom = VSIw->bottom;    /* NCSA 2.5: get the correct scroll rgn */
  784.         else theBottom = VSIw->Rbottom;                /* NCSA 2.5: just use whole screen */
  785.         
  786.         if (VSIcdellines(VSIwn, VSIw->Rtop, theBottom, 1, 1))    /* NCSA 2.5 */
  787.           {
  788.           /* no part of on-screen area is visible */
  789.             if (VSIw->Rtop > -VSIw->numlines)
  790.               /* update bounds of visible region to be consistent
  791.                 with portion of scrollback buffer still being displayed */
  792.               {
  793.                 VSIw->Rtop--;
  794.                 VSIw->Rbottom--;
  795.               }
  796.             else
  797.               {
  798.               /* displaying right from top of scrollback buffer. Topmost
  799.                 line being shown has in fact vanished. Update the display
  800.                 to show this fact. */
  801.                 VSIw->vistop = VSIw->vistop->next;
  802.                 RSdellines(VSIwn, 0, VSIw->Rbottom - VSIw->Rtop, 1, 1);
  803.               } /* if */
  804.           }
  805.         else
  806.             VSIw->vistop = VSIw->vistop->next; /* consistent with changed display */
  807.       /* blank out newly-revealed bottom line */
  808.         tempa = VSIw->attrst[VSIw->lines]->text;
  809.         temp = VSIw->linest[VSIw->lines]->text;
  810.         for (i = 0; i <= VSIw->allwidth; i++)
  811.           {
  812.             *temp++ = ' ';
  813.             *tempa++ = 0;
  814.           } /* for */
  815.       } /* if */
  816. //    tx1 = ty1 = 0;
  817. //    tn = 132;
  818. //    if (!VSIclip(&tx1, &ty1, &tx2, &ty2, &tn, &offset))
  819. //        RSdrawsep(VSIwn, ty1, 1);                    /* Draw Separator */
  820.   } /* VSIscroll */
  821.  
  822. void VSIindex
  823.   (
  824.     void
  825.   )
  826.   /* moves cursor down one line, unless it's at the bottom of
  827.     the scrolling region, in which case scrolls up one. */
  828.   {
  829.     if (VSIw->y == VSIw->bottom)    /* BYU - changed "==" to ">=" and back again */
  830.         VSIscroll();
  831.     else if (VSIw->y < VSIw->lines)     /* BYU  - added "if ... " */
  832.         VSIw->y++;
  833.   } /* VSIindex */
  834.  
  835. void VSIwrapnow(short *xp, short *yp)
  836.   /* checks current cursor position for VSIw to see if
  837.     it's within bounds, wrapping to next line if not.
  838.     Returns correct cursor position in either case in *xp
  839.     and *yp. */
  840.   {
  841.     if (VSIw->x > VSIw->maxwidth) 
  842.       {
  843.         VSIw->x = 0;
  844.         VSIindex();
  845.       } /* if */
  846.     *xp = VSIw->x;
  847.     *yp = VSIw->y;
  848.   } /* VSIwrapnow */
  849.  
  850. void VSIeeol
  851.   (
  852.     void
  853.   )
  854.   /* erases characters to the end of the current line. */
  855.   {
  856.     char
  857.         *tt,
  858.         *ta;
  859.     short
  860.         x1 = VSIw->x,
  861.         y1 = VSIw->y,
  862.         x2 = VSIw->maxwidth,
  863.         y2 = VSIw->y,
  864.         n = -1,
  865.         offset;
  866.     short
  867.         i;
  868.  
  869.     VSIwrapnow(&x1, &y1);
  870.     y2 = y1;
  871.   /* clear out screen line */
  872.     ta = &VSIw->attrst[y1]->text[x1];
  873.     tt = &VSIw->linest[y1]->text[x1];
  874.     for (i = VSIw->allwidth - x1 + 1; i > 0; i--)
  875.       {
  876.         *ta++ = VSIclrattrib;
  877.         *tt++ = ' ';
  878.       }
  879.   /* update display */
  880.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  881.         RSerase(VSIwn, x1, y1, x2, y2);
  882.   } /* VSIeeol */
  883.  
  884. void VSIdelchars
  885.   (
  886.     short x /* how many characters to delete */
  887.   )
  888.   /* deletes characters at the current cursor position onwards,
  889.     moving the remainder of the line to the left. */
  890.   {
  891.     short
  892.         i;
  893.     short
  894.         x1 = VSIw->x,
  895.         y1 = VSIw->y,
  896.         x2 = VSIw->maxwidth,
  897.         y2 = VSIw->y,
  898.         n = -1,
  899.         offset;
  900.     char
  901.         *tempa,
  902.         *temp;
  903.  
  904.     VSIwrapnow(&x1, &y1);
  905.     y2 = y1;
  906.  
  907.     if (x > VSIw->maxwidth)
  908.         x = VSIw->maxwidth;
  909.     tempa = VSIw->attrst[y1]->text;
  910.     temp = VSIw->linest[y1]->text;
  911.     for (i = x1; i <= VSIw->maxwidth - x; i++)
  912.       {
  913.       /* move remainder of line to the left */
  914.         temp[i] = temp[x + i];
  915.         tempa[i] = tempa[x + i];
  916.       }
  917.     for (i = VSIw->maxwidth - x + 1; i <= VSIw->allwidth; i++)
  918.       {
  919.       /* insert blank characters after end of line */
  920.         temp[i] = ' ';
  921.         tempa[i] = VSIclrattrib;
  922.       }
  923.   /* update display */
  924.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset))
  925.       {
  926.         if (VSIw->VSIDC)
  927.             RSdelchars(VSIwn, x1, y1, x);
  928.         else
  929.           /* redraw from cursor position to end of line.
  930.             Trouble is, this isn't going to preserve the
  931.             right attributes. */
  932.             RSdraw
  933.               (
  934.                 VSIwn, x1, y1, VSIw->attrib, n,
  935.                 &VSIw->linest[y1]->text[x1]
  936.               );
  937.       } /* if */
  938.   } /* VSIdelchars */
  939.  
  940. void VSIfreelinelist
  941.   (
  942.     VSlinePtr listhead
  943.   )
  944.   /* frees up the list of line elements pointed to by listhead. */
  945.   {
  946.     register VSlinePtr
  947.         ThisElt, NextElt, ToFree;
  948.  
  949.     ThisElt = listhead;
  950.     ToFree = nil;
  951.     while (true)
  952.       {
  953.         if (ThisElt == nil)
  954.             break;
  955.         NextElt = ThisElt->next;
  956.         if (ThisElt->mem)
  957.           {
  958.             ThisElt->next = ToFree;
  959.             ToFree = ThisElt;
  960.           } /* if */
  961.         ThisElt = NextElt;
  962.         if (ThisElt == listhead)
  963.             break;
  964.       } /* while */
  965.     while (ToFree)
  966.       {
  967.         NextElt = ToFree->next;
  968.         DisposPtr(ToFree->text);
  969.         DisposPtr((Ptr) ToFree);
  970.         ToFree = NextElt;
  971.       } /* while */
  972.   } /* VSIfreelinelist */
  973.  
  974. void VSIfreelines
  975.   (
  976.     void
  977.   )
  978.   /* frees up all the memory allocated for screen and scrollback
  979.     text lines for the current screen. */
  980.   {
  981.     VSIfreelinelist(VSIw->buftop);
  982.   } /* VSIfreelines */
  983.  
  984. void VSIrindex
  985.   (
  986.     void
  987.   )
  988.   /* moves cursor up one line, unless it's at the top of
  989.     the scrolling region, in which case scrolls down one. */
  990.   {
  991.     if (VSIw->y == VSIw->top)
  992.         VSIinslines(1, VSIw->top);
  993.     else
  994.         VSIw->y--;
  995.   } /* VSIrindex */
  996.  
  997. void VSIebol
  998.   (
  999.     void
  1000.   )
  1001.   /* erases characters from beginning of line to cursor. */
  1002.   {
  1003.     char
  1004.         *tt,
  1005.         *ta;
  1006.     short
  1007.         x1 = 0,
  1008.         y1 = VSIw->y,
  1009.         x2 = VSIw->x,
  1010.         y2 = VSIw->y,
  1011.         n = -1,
  1012.         offset;
  1013.     short
  1014.         i;
  1015.  
  1016.     VSIwrapnow(&x2, &y1);
  1017.     y2 = y1;
  1018.   /* clear from beginning of line to cursor */
  1019.     ta = &VSIw->attrst[y1]->text[0];
  1020.     tt = &VSIw->linest[y1]->text[0];
  1021.     for (i = 0; i <= x2; i++)
  1022.       {
  1023.         *ta++ = VSIclrattrib;
  1024.         *tt++ = ' ';
  1025.       }
  1026.   /* update display */
  1027.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1028.         RSerase(VSIwn, x1, y1, x2, y2);
  1029.   } /* VSIebol */
  1030.  
  1031. void VSIel
  1032.   (
  1033.     short s /* line to clear, -ve => line containing cursor */
  1034.   )
  1035.   /* erases the specified line. */
  1036.   {
  1037.     char
  1038.         *tt,
  1039.         *ta;
  1040.     short
  1041.         x1 = 0,
  1042.         y1 = s,
  1043.         x2 = VSIw->maxwidth,
  1044.         y2 = s,
  1045.         n = -1,
  1046.         offset;
  1047.     short
  1048.         i;
  1049.  
  1050.     if (s < 0)
  1051.       {
  1052.         VSIwrapnow(&x1, &y1);
  1053.         s = y2 = y1;
  1054.         x1 = 0;
  1055.       } /* if */
  1056.   /* clear out line */
  1057.     ta = &VSIw->attrst[s]->text[0];
  1058.     tt = &VSIw->linest[s]->text[0];
  1059.     for(i = 0; i <= VSIw->allwidth; i++)
  1060.       {
  1061.         *ta++ = VSIclrattrib;
  1062.         *tt++ = ' ';
  1063.       }
  1064.   /* update display */
  1065.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1066.         RSerase(VSIwn, x1, y1, x2, y2);
  1067.   } /* VSIel */
  1068.  
  1069. void VSIeeos
  1070.   (
  1071.     void
  1072.   )
  1073.   /* erases characters from cursor to end of screen. */
  1074.   {
  1075.     short
  1076.         i;
  1077.     short
  1078.         x1 = 0,
  1079.         y1 = VSIw->y + 1,
  1080.         x2 = VSIw->maxwidth,
  1081.         y2 = VSIw->lines,
  1082.         n = -1,
  1083.         offset;
  1084.  
  1085.     VSIwrapnow(&x1, &y1);
  1086.     y1++;
  1087.     x1 = 0;
  1088.  
  1089.     i = y1;
  1090.  
  1091.      if (VSIw->forcesave)        /* NCSA 2.5 */
  1092.         VSIscroff();            /* NCSA 2.5 */
  1093.  
  1094.  
  1095.   /* erase complete lines from screen */
  1096.     if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1097.         RSerase(VSIwn, x1, y1, x2, y2);
  1098.   /* blank out current line from cursor to end */
  1099.     VSIeeol(); /* this also erases the partial line on-screen */
  1100.   /* blank out remaining lines to end of screen */
  1101.     while (i <= VSIw->lines)
  1102.       {
  1103.         VSIelo(i);
  1104.         i++;
  1105.       } /* while */
  1106.     if (VSIw->y < VSIw->lines && (VSIw->x <= VSIw->maxwidth))
  1107.       /* erase the partial line (what--again??) */
  1108.         if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1109.             RSerase(VSIwn, x1, y1, x2, y2);
  1110.   } /* VSIeeos */
  1111.  
  1112. void VSIebos
  1113.   (
  1114.     void
  1115.   )
  1116.   /* erases characters from beginning of screen to cursor. */
  1117.   {
  1118.     short
  1119.         i;
  1120.     short
  1121.         x1,
  1122.         y1,
  1123.         x2 = VSIw->maxwidth,
  1124.         y2,
  1125.         n = -1,
  1126.         offset;
  1127.  
  1128.     VSIwrapnow(&x1, &y1);
  1129.     y2 = y1 - 1;
  1130.     x1 = 0;
  1131.     y1 = 0;
  1132.   /* blank out current line from beginning to cursor */
  1133.     VSIebol(); /* this also erases the partial line on-screen */
  1134.     i = 0;
  1135.   /* blank out remaining lines from beginning of screen to previous line */
  1136.     while (i < (y2 + 1))
  1137.       {
  1138.         VSIelo(i);
  1139.         i++;
  1140.       } /* while */
  1141.     if (y2 >= 0)
  1142.       /* erase the partial line (what--again??) */
  1143.         if (!VSIclip(&x1, &y1, &x2, &y2, &n, &offset)) 
  1144.             RSerase(VSIwn, x1, y1, x2, y2);
  1145.   } /* VSIebos */
  1146.  
  1147. void VSIrange
  1148.   (
  1149.     void
  1150.   )
  1151.   /* constrains cursor position to valid range (somewhere on the screen). */
  1152.   {
  1153.     short
  1154.         wrap = 0;
  1155.     if (VSIw->DECAWM)
  1156.         wrap = 1;
  1157.     if (VSIw->x < 0)
  1158.         VSIw->x = 0;
  1159.     if (VSIw->x > (VSIw->maxwidth + wrap))
  1160.         VSIw->x = VSIw->maxwidth + wrap;
  1161.     if (VSIw->y < 0)
  1162.         VSIw->y = 0;
  1163.     if (VSIw->y > VSIw->lines)
  1164.         VSIw->y = VSIw->lines;
  1165.   } /* VSIrange */
  1166.  
  1167. void VTsendpos
  1168.   (
  1169.     void
  1170.   )
  1171.   /* sends an escape sequence representing the current cursor position. */
  1172.   {
  1173.     char
  1174.         tempbuf[19];
  1175.     short
  1176.         x = VSIw->x,
  1177.         y = VSIw->y;
  1178.  
  1179.     if (x > VSIw->maxwidth)
  1180.       {
  1181.       /* autowrap pending */
  1182.         x = 0;
  1183.         y++;
  1184.       }
  1185.     if (y > VSIw->lines)
  1186.       /* scroll pending (because of the autowrap) */
  1187.         y = VSIw->lines;
  1188.  
  1189.     sprintf(tempbuf, "\033[%d;%dR", y + 1, x + 1);
  1190.     RSsendstring(VSIwn, tempbuf, strlen(tempbuf));
  1191.   } /* VTsendpos */
  1192.  
  1193. void VTsendstat
  1194.   (
  1195.     void
  1196.   )
  1197.   /* sends the terminal status string. */
  1198.   {
  1199.     RSsendstring(VSIwn, "\033[0n", 4);
  1200.   } /* VTsendstat */
  1201.  
  1202. void VTsendident
  1203.   (
  1204.     void
  1205.   )
  1206.   /* sends an appropriate terminal identification sequence. */
  1207.   {
  1208. #ifdef VT100RESP
  1209.     if (VSIw->allwidth > 80)
  1210.         RSsendstring(VSIwn, "\033[?4;6c", 7);
  1211.     else
  1212.         RSsendstring(VSIwn, "\033[?1;6c", 7);
  1213. #endif VT100RESP
  1214.     if (screens[findbyVS(VSIwn)].vtemulation)
  1215.         RSsendstring(VSIwn, "\033[?62;1;6c", 10);            /* BYU 2.4.12 - VT200-series*/
  1216.     else                                                    /* BYU 2.4.12 */
  1217.         RSsendstring(VSIwn, "\033[?6c", 5);                    /* BYU 2.4.12 - VT102 */
  1218.   } /* VTsendident */
  1219.  
  1220. void VTalign
  1221.   (
  1222.     void
  1223.   )
  1224.   /* fills screen with uppercase "E"s, for checking screen alignment. */
  1225.   /* Yeah, right. */
  1226.   {
  1227.     char *tt;
  1228.     short i, j;
  1229.  
  1230.     VSIes();        /* erase the screen */
  1231.     for (j = 0; j < VSIw->lines; j++)
  1232.       {
  1233.         tt = &VSIw->linest[j]->text[0];
  1234.         for (i = 0; i <= VSIw->maxwidth; i++)
  1235.             *tt++ = 'E';
  1236.       } /* for */
  1237.   /* update the display */
  1238.     VSredraw(VSIwn, 0, 0,
  1239.         (VSIw->Rright - VSIw->Rleft), (VSIw->Rbottom - VSIw->Rtop));
  1240.   } /* VTalign */
  1241.  
  1242. void VSIapclear
  1243.   (
  1244.     void
  1245.   )
  1246.   /* initializes all the parameters for the current control
  1247.     sequence, and the current param index, to zero. */
  1248.   {
  1249.     short
  1250.         parmptr = maxparms;
  1251.     while (--parmptr >= 0)
  1252.         VSIw->parms[parmptr] = -1;
  1253.     VSIw->parmptr = 0;
  1254.   } /* VSIapclear */
  1255.  
  1256. void VSIsetoption
  1257.   (
  1258.     short toggle /* 1 => set, 0 => reset */
  1259.   )
  1260.   /* sets/resets various options, as specified by the parms in
  1261.     the current control sequence. Note that this implementation
  1262.     will not set/reset more than one option at a time! */
  1263.   {
  1264.     short
  1265.         WindWidth = VSIw->Rright - VSIw->Rleft;
  1266.  
  1267.     switch (VSIw->parms[0])
  1268.       {
  1269.         case -2: /* DEC-private control sequence */
  1270.             switch (VSIw->parms[1])
  1271.               {
  1272.                 case 1: /* cursor-key mode */
  1273.                     VSIw->DECCKM = toggle;
  1274.                     break;
  1275.                 case 3: /* 80/132 columns */
  1276.                     VSIw->x = VSIw->y = 0; /* home cursor */
  1277.                     VSIes(); /* and clear screen */
  1278.                     if (toggle)    /* 132 column mode */
  1279.                         {                                                /* NCSA: SB */
  1280.                         VSIw->maxwidth = 131;                            /* NCSA: SB */
  1281.                         RScalcwsize(VSIwn,132);                            /* NCSA: SB */
  1282.                         }                                                /* NCSA: SB */
  1283.                     else                                                /* NCSA: SB */
  1284.                         {                                                /* NCSA: SB */
  1285.                         VSIw->maxwidth = 79;                            /* NCSA: SB */
  1286.                         RScalcwsize(VSIwn,80);                            /* NCSA: SB */
  1287.                         }                                                /* NCSA: SB */
  1288.                   /* update scroll bars */                                    
  1289.                     RSmargininfo(VSIwn, VSIw->maxwidth, VSIw->Rleft);    /* NCSA: SB */    
  1290.                     break;
  1291.                     
  1292. /* NCSA: SB -  this next one will allow us to flip the foreground and        */
  1293. /*        background colors.                                                    */
  1294.                 case 5:                                            /* NCSA: SB - screen mode */
  1295.                     RSbackground(VSIwn,toggle);
  1296.                     break;
  1297.                 
  1298.                 case 6: /* origin mode */
  1299.                     VSIw->DECORG = toggle;
  1300.                     break;
  1301.                 case 7: /* autowrap mode */
  1302.                     VSIw->DECAWM = toggle;
  1303.                     break;
  1304.                 default:
  1305.                     break;
  1306.               } /* switch */
  1307.             break;
  1308.         case  4: /* insert/replace character writing mode */
  1309.             VSIw->IRM = toggle;
  1310.             break;
  1311.         default:
  1312.             break;
  1313.       } /* switch */
  1314.   } /* VSIsetoption */
  1315.  
  1316. short VSItab
  1317.   (
  1318.     void
  1319.   )
  1320.   /* advances VSIw->x to the next tab position. */
  1321.   {
  1322.     if (VSIw->x >= VSIw->maxwidth)
  1323.       {
  1324.       /* already at right margin */
  1325.         VSIw->x = VSIw->maxwidth;
  1326.         return(0);
  1327.       } /* if */
  1328.     VSIw->x++; /* advance at least one position */
  1329.     while ((VSIw->tabs[VSIw->x] != 'x') && (VSIw->x < VSIw->maxwidth))
  1330.         VSIw->x++;
  1331.     return(0);
  1332.   } /* VSItab */
  1333.  
  1334. void VSIinschar
  1335.   (
  1336.     short x /* number of blanks to insert */
  1337.   )
  1338.   /* inserts the specified number of blank characters at the
  1339.     current cursor position, moving the rest of the line along,
  1340.     losing any characters that fall off the right margin.
  1341.     Does not update the display. */
  1342.   {
  1343.     short i, j;
  1344.     char *tempa, *temp;
  1345.  
  1346.     VSIwrapnow(&i, &j);
  1347.  
  1348.     tempa = VSIw->attrst[VSIw->y]->text;
  1349.     temp = VSIw->linest[VSIw->y]->text;
  1350.     for (i = VSIw->maxwidth - x; i >= VSIw->x; i--)
  1351.       {
  1352.       /* move along remaining characters on line */
  1353.         temp[x + i] =temp[i];
  1354.         tempa[x + i] = tempa[i];
  1355.       } /* for */
  1356.     for (i = VSIw->x; i < VSIw->x + x; i++)
  1357.       {
  1358.       /* insert appropriate number of blanks */
  1359.         temp[i] = ' ';
  1360.         tempa[i] = VSIclrattrib;
  1361.       } /* for */
  1362.   } /* VSIinschar */
  1363.  
  1364. void VSIinsstring
  1365.   (
  1366.     short len,
  1367.     char *start
  1368.   )
  1369.   /* updates the screen to show insertion of a string of characters
  1370.     at the current cursor position. The text has already been
  1371.     inserted into the screen buffer. Also, the cursor position has
  1372.     already been updated, so the part needing redrawing begins at column
  1373.     (VSIw->x - len). */
  1374.   {
  1375.     if (VSIw->VSIDC)
  1376.         RSinsstring(VSIwn, VSIw->x - len, VSIw->y,
  1377.             VSIw->attrib, len, start);
  1378.     else
  1379.       /* redraw from beginning of text insertion to end of line.
  1380.         Trouble is, this isn't going to preserve the right attributes. */
  1381.         RSdraw(VSIwn, VSIw->x - len, VSIw->y, VSIw->attrib,
  1382.             VSIw->maxwidth - VSIw->x + len + 1, start);
  1383.   } /* VSIinsstring */
  1384.  
  1385. void VSIsave
  1386.   (
  1387.     void
  1388.   )
  1389.   /* saves the current cursor position and attribute settings. */
  1390.   {
  1391.     VSIw->Px = VSIw->x;
  1392.     VSIw->Py = VSIw->y;
  1393.     VSIw->Pattrib = VSIw->attrib;
  1394.   } /* VSIsave */
  1395.  
  1396. void VSIrestore
  1397.   (
  1398.     void
  1399.   )
  1400.   /* restores the last-saved cursor position and attribute settings. */
  1401.   {
  1402.     if (VSIw->Pattrib < 0)
  1403.       /* no previous save */
  1404.         return;
  1405.         
  1406.     VSIw->x = VSIw->Px;
  1407.     VSIw->y = VSIw->Py;
  1408.     VSIrange();
  1409.     VSIw->attrib = VSinattr(VSIw->Pattrib); /* hmm, this will clear the graphics character set selection */
  1410.   } /* VSIrestore */
  1411.  
  1412. void VSIdraw
  1413.   (
  1414.     short VSIwn, /* window number */
  1415.     short x, /* starting column */
  1416.     short y, /* line on which to draw */
  1417.     short a, /* text attributes */
  1418.     short len, /* length of text to draw */
  1419.     char *c /* pointer to text */
  1420.   )
  1421.   /* displays a piece of text (assumed to fit on a single line) on a
  1422.     screen, using the specified attributes, and clipping to the
  1423.     current visible region. Highlights any part of the text lying
  1424.     within the current selection. */
  1425.   {
  1426.     short x2, y2, offset;
  1427.  
  1428.     if (!VSIclip(&x, &y, &x2, &y2, &len, &offset))
  1429.         RSdraw(VSIwn, x, y, a, len, (char *) (c + offset));    /* BYU LSC */
  1430.   } /* VSIdraw */
  1431.